package com.api.controller;

import com.api.mapper.*;
import com.api.pojo.Task;
import com.api.pojo.User;
import com.api.pojo.UserTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    TaskMapper tm;

    @Autowired
    UserTaskMapper utm;

    @Autowired
    UserMapper um;

    @Autowired
    BuildingMapper bm;

    @Autowired
    ChamberMapper cm;

    @PostMapping("/create")
    public Map<String, Object> create(Task task) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        tm.create(task);
        List<Integer> uids = tm.getUserIds();
        uids.forEach(e -> {
            utm.create(e, task.getId());
        });

        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @DeleteMapping("/remove")
    public Map<String, Object> remove(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        tm.remove(id);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @PutMapping("/update")
    public Map<String, Object> update(Task task) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        tm.update(task);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }
    
    @GetMapping("/get")
    public Map<String, Object> get(String name, Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        pageNo = --pageNo * pageSize;
        List<Task> list = name == null ? tm.get(pageNo, pageSize) : tm.getByName("%" + name + "%", pageNo, pageSize);
        list.forEach(e -> {
            List<Integer> cs = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                cs.add(utm.count(e.getId(), i));
            }
            e.setUser(um.getById(e.getUid()));
            e.setCount(cs);
        });

        map.put("data", list);
        map.put("total", name == null ? tm.count() : tm.countByName("%" + name + "%"));
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @GetMapping("/getInfo")
    public Map<String, Object> getInfo(Integer id, Integer state, Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        pageNo = --pageNo * pageSize;
        List<UserTask> list = utm.getInfo(id, state, pageNo, pageSize);
        list.forEach(e -> {
            User user = um.getById(e.getUid());
            user.setBuilding(bm.getById(user.getBid()));
            user.setChamber(cm.getById(user.getCid()));
            e.setUser(user);
        });
        map.put("data", list);
        map.put("total", utm.countInfo(id, state));
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }


}
